//
//  PlayView.m
//  27-Block练习-播放按钮
//
//  Created by dllo on 16/8/19.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "PlayView.h"
#import "UIView+Frame.h"
#import "Button_image.h"
@interface PlayView ()
@property (nonatomic, strong) Button_image *playButton;
//@property (nonatomic, strong) UIButton *stopButton;
@property (nonatomic, strong) Button_image *nextButton;
@property (nonatomic, strong) Button_image *lastButton;

@property (nonatomic, copy) PlayButton play;
@property (nonatomic, copy) PauseButton pause;
@property (nonatomic, copy) NextButton next;
@property (nonatomic, copy) LastButton last;

@end

@implementation PlayView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
      [self setUp];
    }
    return self;
}
- (void)setUp {
    _playButton = [Button_image buttonWithType:UIButtonTypeCustom];
    _nextButton = [Button_image buttonWithType:UIButtonTypeCustom];
    _lastButton = [Button_image buttonWithType:UIButtonTypeCustom];

    _playButton = [self buttonImageWithName:@"player_btn_play_normal" action:@selector(playButton:)];
    _lastButton = [self buttonImageWithName:@"hp_player_btn_pre_normal" action:@selector(lastButton:)];
    _nextButton = [self buttonImageWithName:@"hp_player_btn_next_normal" action:@selector(nextButton:)];
   }

- (Button_image *)buttonImageWithName:(NSString *)imageName action:(SEL)action {
    Button_image * button = [Button_image buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    return button;
}
#pragma mark - 实现block协议
- (void)play:(void (^)(Button_image *))play pause:(void (^)(Button_image *))pause {
    self.play = play;
    self.pause = pause;
}
- (void)last:(LastButton)last next:(NextButton)next {
    self.last = last;
    self.next = next;
}
#pragma mark - playButton的点击方法的实现
- (void)playButton:(Button_image *)playButton {
    [_playButton setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
    [_playButton removeTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
    [_playButton addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    // 调用block方法
    self.play(playButton);
    
}
#pragma mark -  pauseButton的点击方法的实现
- (void)pause:(Button_image *)pause {
    [_playButton setImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    [_playButton removeTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [_playButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
    // 调用block方法
    self.pause(pause);
}

- (void)nextButton:(Button_image *)nextButton {
    _lastButton = [self buttonImageWithName:@"hp_player_btn_pre_normal" action:@selector(lastButton:)];
    self.next(nextButton);
}
- (void)lastButton:(Button_image *)lastButton {
     _nextButton = [self buttonImageWithName:@"hp_player_btn_next_normal" action:@selector(nextButton:)];
    self.last(lastButton);
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _playButton.dm_width = 60;
    _playButton.dm_height = 60;
    _playButton.dm_centerX = self.dm_width / 2;
    _playButton.dm_centerY = self.dm_height / 2;
    
    _lastButton.dm_width = 60;
    _lastButton.dm_height = 60;
    _lastButton.dm_centerY = _playButton.dm_centerY;
    
    _lastButton.dm_centerX = _playButton.dm_centerX / 2;
    
    _nextButton.dm_width = 60;
    _nextButton.dm_height = 60;
    _nextButton.dm_centerY = _playButton.dm_centerY;
    
    _nextButton.dm_centerX = _playButton.dm_centerX / 2 * 3;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
